//
//  RichViewController.m
//  YLProject
//
//  Created by 王跃 on 2018/1/10.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "RichViewController.h"
#import "Rich_HomeCell.h"
#import "Rick_HomeHeaderView.h"
#import "Rank_GiftListSortView.h"
#import "ZZPopoverWindow.h"
#import "Rick_HomeModel.h"
@interface RichViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)Rick_HomeHeaderView *headerView;
@property(nonatomic,strong)ZZPopoverWindow *popover;
@end

@implementation RichViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNav];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.tableHeaderView = self.headerView;
}
-(void)setUpNav{
    self.navigationItem.title = @"财富";
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 5,34,34);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"排行_highlight"] forState:UIControlStateNormal];
    [[rightButton  rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"财富右按钮");
        Rank_GiftListSortView *sortView = [Rank_GiftListSortView popViewBlock:^(NSString *att){
            if ([att isEqualToString:@"integralGift"]) {
                YLLog(@"我的红包");
            }else{
                YLLog(@"我的礼物");
            }
        }];
        [sortView changeTitle:@"我的红包" twoString:@"我的礼物"];
        sortView.backgroundColor = [UIColor clearColor];
        self.popover                    = [[ZZPopoverWindow alloc] init];
        // self.popover.popoverPosition = ZZPopoverPositionRight; // 指定contentView在箭头的哪个方向，默认自己会选择
        self.popover.backgroundColor = [UIColor blackColor]; // 指定弹框背景色
        // self.popover.showArrow       = NO; // 是否显示箭头，默认YES
        self.popover.contentView        = sortView; // 指定contentView
        self.popover.didShowHandler = ^() {
            NSLog(@"Did show");
        };
        self.popover.didDismissHandler = ^() {
            NSLog(@"Did dismiss");
        };
        [self.popover showAtView:rightButton];
    }];
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc]initWithCustomView:rightButton]];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 5,34,34);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"排行_highlight"] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"排行"] forState:UIControlStateSelected];
    [[leftButton  rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"财富左按钮");
        
    }];
//    leftButton.hidden = YES;
    self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc]initWithCustomView:leftButton]];
}

#pragma mark -- UITableViewDelegate / UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.dataSource.count;
    }
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section == 1){return 20;}
    return .01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc]init];
    UIView *whView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, Screen_Width, 40)];
    whView.backgroundColor = [UIColor whiteColor];
    [backView addSubview:whView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, Screen_Width - 30 / 2 ,40 )];
    label.font = YLFont(15);
    label.textColor = [UIColor blackColor];
    if (section == 0) {
        label.text = @"获取能量";
    }else{
        label.text = @"能量明细";
    }
    
    [whView addSubview:label];
    return backView;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Rich_HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([Rich_HomeCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell initWithModel:self.dataSource[indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Rick_HomeModel *model = self.dataSource[indexPath.row];
    id<IProductProtocol> product = [[ViewFactory shareInstance] createProduct:model.att];
    if ([product isKindOfClass:[UIViewController class]]) {
        BaseViewController *controller = (BaseViewController *)product;
        controller.hidesBottomBarWhenPushed = YES;
        controller.navigationItem.title = model.navTitle;
        [controller method];
        [self.navigationController pushViewController:controller animated:YES];
    }
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - TAB_BAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([Rich_HomeCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([Rich_HomeCell class])];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = kHEXCOLOR(@"f4f5f7");
    }
    return _tableView;
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithArray:[Rick_HomeModel modelWithArrayForValue]];
    }
    return _dataSource;
}
-(Rick_HomeHeaderView *)headerView{
    if (!_headerView) {
//        UINib *nib = [UINib nibWithNibName:NSStringFromClass(Rick_HomeHeaderView.class) bundle:nil];
//        NSArray *views = [nib instantiateWithOwner:nil options:nil];
//        _headerView = views.firstObject;
        
    _headerView = [Rick_HomeHeaderView headerView];
    }
    return _headerView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
